<?php

namespace app\adminapi\controller;

use think\Controller;
use think\facade\Request;
use app\adminapi\model\Goods as goodstable;

class Goods extends BaseApi
{
    public function index(){
        // $this->ok("sbhd");
        //接受参数
        $param=Request::param();
        //查询数据
        $pageSize=$param['pageSize'];
        if(!empty($param['keyword'])){
            $keyword=$param['keyword'];
            // $data=roletable::where('role_name','like',"%$keyword%")->paginate($pageSize);
            $data=goodstable::alias('t1')
            ->join('pyg_category t2','t1.`cate_id`=t2.`id`','inner')
            ->join('pyg_brand t3','t1.`brand_id`=t3.`id`','left')
            ->field('t1.*,t2.cate_name,t3.name')
            ->where('goods_name','like',"%$keyword%")
            ->paginate($pageSize);

            //方法2失败的
            // $data=goodstable::alias('t1')
            // ->join('pyg_category t2','inner')->on('t1.`cate_id`=t2.`id`')
            // ->join('pyg_brand t3','left')->on('t1.`brand_id`=t3.`id`')
            // ->field('t1.*,t2.cate_name,t3.name')
            // ->where('goods_name','like',"%$keyword%")
            // ->paginate($pageSize);
        }else{
            //$data=roletable::paginate($pageSize);
            $data=goodstable::alias('t1')
            ->join('pyg_category t2','t1.`cate_id`=t2.`id`')
            ->join('pyg_brand t3','t1.`brand_id`=t3.`id`')
            ->field('t1.*,t2.cate_name,t3.name')
            ->paginate($pageSize);

            //方法2失败的    
            // $data=goodstable::alias('t1')
            // ->join('pyg_category t2','inner')->on('t1.`cate_id`=t2.`id`')
            // ->join('pyg_brand t3','left')->on('t1.`brand_id`=t3.`id`')
            // ->field('t1.*,t2.cate_name,t3.name')
            // ->paginate($pageSize);
        }
        $this->ok($data);

    }
    // 单文件上传
    public function upload(){
       //获取表单上传文件，例如上传01.jpg
       $file=request()->file('file');
       //移动文件
       $info =$file->move('../public/uploads/');
       if($info){
           //成功上传后 获取上传信息
           //输出jpg
        //    $this->ok('文件上传成功');
        $logoSrc='/uploads//'.$info->getSaveName();
        $this->ok($logoSrc);
       }else{
           //上传失败获取错误信息
           $this->fail("上传文件失败".$file->getError());
       }
    }
    // 添加商品
    public function add(){
    // 接收参数
    $param=Request::param();
    $this->ok($param);
    // 验证
    // 将数据添加到数据库
    $data=['goods_name'=>$param['goodsname']];
    goodsTable::create($data,true);
    // 返回
    
    }
}
